<?php

class Article_Collection extends Collection
{
	public function Article_Collection()
	{
		parent::Collection("article", "id");
	}

	public function getByUrl($str)
	{
		$sql = new SQLAdapt();
		$req = "select * from article where url_title = '$str'";
		$res = $sql->query($req);
		$res = $res->fetch();
			
		if($res != null)
		{
			$constructeur = "Article_Modele::getRef";
			// On construit la liste des param�tres
			for($i=0;$i<$this->_nbField;$i++)
			$param[$i] = $res[$i];
			// On construit l'occurence
			$occurence = call_user_func_array($constructeur, $param);
			return $occurence;
		}
		else
		return null;
	}

	public function getNbArticle($id = null)
	{
		$sql = new SQLAdapt();
		if($id != null)
		$req = "select count(id) as nb from article where isPublished = 1 and categorieId = $id";
		else
		$req = "select count(id) as nb from article where isPublished = 1";
		$res = $sql->query($req);
		$res = $res->fetch();
		return $res[0];
	}

	private function toArray($req)
	{
		$adapt = new SQLAdapt();
		$resArray = array();
		$cpt = 0;

		$res = $adapt->query($req);

		while($obj = $res->fetch())
		{
			$resArray[$cpt] = new Article_Modele(
			$obj[0],
			$obj[1],
			$obj[2],
			$obj[3],
			$obj[4],
			$obj[5],
			$obj[6],
			$obj[7],
			$obj[8],
			$obj[9]
			);
			$cpt++;
		}

		return $resArray;
	}

	public function getPublicArticles() {

		$req = "SELECT art.id, art.title, art.url_title, art.description, art.keywords, art.content, art.creation_date, art.update_date, art.isPublished, art.categorieId
		          FROM article art, categorie cat 
		          WHERE art.categorieId = cat.id
		          AND cat.isPublic = 1";

		return $this->toArray($req);
	}
}

?>
